
module DrunkenMaster

  
  class FormField < Widget
    attr_accessor :data
    def initialize name = nil, data = nil
      super()
      self.name = name
      self.data = data
      #element.change = "$(this.parentNode).load('#{link(:ajax_validate)}/'+this.value); return false"
    end
    
    def recieve data
      #puts "setting my (#{whole_name}) data to #{data[whole_name]}"
      if whole_name
        self.data = data[whole_name] || data[whole_name.to_sym]
      else
        raise "I can't recieve, because I have no name!"
      end
    end
    
    def ajax_validate
      element.value = @context.perspective.path.last
        validate
      render @context
    end

    def validate
      element.style = is_valid ? 'color:green' : 'color:red'
    end

    def is_valid
      true
    end
  end    

  class InputField < FormField    
    def initialize *args
      super(*args)
      self.tagName = 'input'
      self.type = 'text'
    end
    
    def name= name
      super(name)
      attributes[:name] = self.name
    end
    
    def data= data
      self.value = data
    end
    
    def data
      self.value
    end
  end
end
